function graphsVec=plotSurprises(pathMat,surprisePath,NPlot,inStru,nameStru)
% =========================================================================
%% plotTransitions(varMat,obsnames,shonames,title,offset);
%
% Given and [Ny Nper+1 Nmod] matrix of transition paths and 
% an [Ny Nper+1] matrix of surprisePath plot the first NPer periods 
%
% Plot them for each series across different models 
% 
%% Inputs
% *pathMat*         [Ny Nper+1 Nmod] matrix of transitions 
% *surpriseMat*     [Ny Nper+1     ] matrix of transitions 
%  
% 
% *inStru*     Structure with all inputs for the graph, 
%              including 
%              .NFigCols     Columns per figure (default is 2) 
%              .NFigRows     Rows per figure (default is min(4,Ny/2) ) 
%
% *nameStru*    .rows
%               .pages
%
%
%% Outputs 
% graphsVec: Vector handles 
%
% Alejandro Justiniano January 21st 2010
% =========================================================================
[Nstates Nper Nmod]=size(pathMat); 
%% 1. Defaults
if isempty(inStru)==true 
    inStru.flagEmpty=true; 
end 
[~,inStru]=ch_field(inStru,'flagColor',1); 
[~,inStru]=ch_field(inStru,'colors',[0 0 0;0 0 1;1 0 0;0 0.5 0;0.55 0.55 0.55]);
[~,inStru]=ch_field(inStru,'lineStyles',{'-','-.',':','--','-'}); 
[~,inStru]=ch_field(inStru,'markers',{'none','none','^','none','*','s'}); 
[~,inStru]=ch_field(inStru,'markersSize',[1 1.5 2.5 4 4.5]); 
[~,inStru]=ch_field(inStru,'xOffset',1e-5); 
[~,inStru]=ch_field(inStru,'YGridOn',0); 
[~,inStru]=ch_field(inStru,'XGridOn',0); 
[~,inStru]=ch_field(inStru,'yOffset',1e-5); 
[~,inStru]=ch_field(inStru,'flagBoxOff',1); 
if Nmod > 3
    [~,inStru]=ch_field(inStru,'lineWidths',[1.5 1.5 2.25 1 1]);
else
    [~,inStru]=ch_field(inStru,'lineWidths',[2 2 2.5]);
end
[~,inStru]=ch_field(inStru,'suptitle',[]); 
[~,inStru]=ch_field(inStru,'suptitleFlush','left'); 
[~,inStru]=ch_field(inStru,'xaxis',(0:Nper-1)'); 
[~,inStru]=ch_field(inStru,'paperPosition',[0.0 0.0 8.5 11]); 
[~,inStru]=ch_field(inStru,'legendFontSize',14); 
% NRows and NCols defined below 
%% Requires \, asking to replace '\\' with '\'
nameStru.rows=regexprep(nameStru.rows,'\$',''); 
nameStru.rows=regexprep(nameStru.rows,'{',''); 
nameStru.rows=regexprep(nameStru.rows,'}',''); 
nameStru.rows=regexprep(nameStru.rows,'\\\','\'); 

nameStru.pages=regexprep(nameStru.pages,'\$',''); 
nameStru.pages=regexprep(nameStru.pages,'\\\','\'); 
nameStru.pages=regexprep(nameStru.pages,'{',''); 
nameStru.pages=regexprep(nameStru.pages,'}',''); 

[~,inStru]=ch_field(inStru,'legend',nameStru.pages); 

inStru=plotAssignDefaults(inStru);
%% 2. Check sizes
if length(nameStru.rows)~=Nstates; 
    error('nameStru.rows does not match number of rows in pathMat'); 
end 
%if length(nameStru.pages)~=Nmod; 
%    error('nameStru.pages does not match number of pages in pathMat'); 
%end 
% if isfield(inStru,'legend')==false; 
%     error('Need provide inStru.legend'); 
% end 
%% 3. Organization of Plots
[~,inStru]=ch_field(inStru,'NFigCols',2); 
[~,inStru]=ch_field(inStru,'NFigRows',min(4,ceil(Nstates/inStru.NFigCols))); 
if ~isempty(inStru.axesFontSize); 
    set(0,'DefaultAxesFontSize',inStru.axesFontSize);
end 

%% 4. Graph Settings 
NOuter=Nstates; 
NRows=inStru.NFigRows;
NCols=inStru.NFigCols;

graphsVec =zeros(ceil(NOuter/(NRows*NCols)),1);

ISubplots=0;
IGraphs  =0;
ICounter =0;
xaxis=(0:NPlot-1);

for jj=1:NOuter; 
        
    if ISubplots==0
        IGraphs=IGraphs+1;
        graphsVec(IGraphs,1)=figure;
    end
    ISubplots=ISubplots+1;
    ICounter =ICounter+1;
    
    subplot(NRows,NCols,ISubplots);
    
    if ISubplots==NRows*NCols || jj==NOuter
        inStru.flagNoLegend=0;
    else
        inStru.flagNoLegend=1;
    end

    %% Plot first NPlot 
    for ii=1:Nmod;
        plotHandle=plot( xaxis(ii:end) , squeeze( pathMat(jj,1:(NPlot-ii+1),ii) ) );
        set(plotHandle,'Color',[0.5 0.5 0.5],'LineStyle','--','LineWidth',1.5);
        if ii==1;hold on; end
    end
    plotHandle=plot( xaxis ,surprisePath(jj,1:NPlot) );
    set(plotHandle,'Color',[0. 0. 0.],'LineStyle','-','LineWidth',1.5);
    axis tight;
    set(gca,'Box','off')
    title(inStru.names.rows{jj},'FontSize',12); 
       
    if jj==NOuter || ISubplots==NRows*NCols;
        ISubplots=0;
        % Flush Suptitle?
        %tempSuptit='Transitions'; 
        tempSuptit=''; 
        if isempty(inStru.suptitle)==false;
            if strmatch(lower(inStru.suptitleFlush),'left')==1;
                tempSuptit=[char(inStru.suptitle),char(tempSuptit)];
            else
                tempSuptit=[char(tempSuptit),char(inStru.suptitle)];
            end
            if size(graphsVec,1)==1
                supHand=suptitle(tempSuptit);
            else
                supHand=suptitle([tempSuptit,'   (',num2str(IGraphs),' of ',...
                    num2str(size(graphsVec,1)),')']);
            end
            set(supHand,'FontSize',inStru.suptitleFontSize,'FontName',inStru.FontName,...
                'FontWeight','Bold');
        end
        set(graphsVec(IGraphs,1),'PaperPosition',inStru.paperPosition,'PaperUnits','in',...
            'PaperPositionMode','manual')
    end
end
if ~isempty(inStru.axesFontSize);
    set(0,'DefaultAxesFontSize','remove');
end
end 